package com.lloydfinch.composetour.ui.page

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel

/**
 * Name: ZhuJIangTour
 * Author: lloydfinch
 * Function: ZhuJIangTour
 * Date: 2021/12/1 5:19 下午
 * Modify: lloydfinch 2021/12/1 5:19 下午
 */

@Composable
fun Counter() {
    // 获取Context
    val context = LocalContext.current
    val viewModel: OverViewModel = viewModel()
    val count by viewModel.count.observeAsState(0)
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = "$count", modifier = Modifier.padding(10.dp))
        Button(onClick = { viewModel.changeCount(count + 1) }) {
            Text(text = "Add Count")
        }
    }

}


class OverViewModel : ViewModel() {

    private val _count = MutableLiveData(0)
    val count: LiveData<Int> = _count

    fun changeCount(count: Int) {
        this._count.postValue(count)
    }
}
